package org.bdware.repository.examples;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.apache.log4j.Logger;
import org.bdware.doip.core.crypto.GlobalCertifications;
import org.bdware.irp.stateinfo.DoipServiceStateInfo;
import org.bdware.doip.core.utils.GlobalConfigurations;
import org.bdware.doip.endpoint.doipServer.*;
import org.bdware.irp.client.GlobalIrpClient;
import org.bdware.doip.implementation.DoStorageRocksDBImpl;
import org.bdware.repository.DoipServiceConfig;
import org.bdware.repository.handler.SimpleRepositoryHandler;
import org.bouncycastle.jce.provider.BouncyCastleProvider;

import java.security.Security;
import java.util.List;

public class SimpleRepositoryMain {

    static Logger logger = Logger.getLogger(SimpleRepositoryMain.class);

    /**
     * DOIP Service Information
     */
    public static String listeners = "[{\"url\":\"tcp://127.0.0.1:1717\",\"protocolVersion\":\"2.1\",\"messageFormat\":\"packet\"}]";
//    public static String listeners = "[{\"url\":\"tls://127.0.0.1:1720\",\"protocolVersion\":\"2.0\",\"messageFormat\":\"delimiter\"}]";
    public static String repoID = "86.5000.470/doip.localSimpleRepo_bdw";
    public static String userID = "86.5000.470/dou.TEST";
    public static String jwkPath = "./keys/jwk/dou.TEST.jwk";
    public static String serviceDescription = "test local Simple TCP Repository";
    public static String serviceName = "TestSimple";
    public static String IRSAddress = "http://127.0.0.1:10001/";
    public static String type="repository";


    public static void main(String[] args) {

        DoipServiceInfo info;
        try {
            init();
            info = new DoipServiceInfo(
                    repoID,
                    userID,
                    type,
                    new Gson().fromJson(listeners, new TypeToken<List<DoipListenerInfo>>() {
                    }.getType()));
            info.serviceName = serviceName;
            info.serviceDescription = serviceDescription;

            DoipServiceStateInfo repoHR = info.toServiceHandleRecord();
            logger.debug(new Gson().toJson(repoHR));
            if (repoHR.identifier == null || repoHR.identifier.equals("")) {
                String newHandle = GlobalIrpClient.getGlobalClient().register(repoHR);
                logger.info("[HandleService] success register to LHS, handle: " + newHandle);
                repoID = info.id = newHandle;
            } else {
                String LHSResp = GlobalIrpClient.getGlobalClient().reRegister(repoHR);
                logger.info("[HandleService] success register to LHS, handle: " + LHSResp);
            }
            DoipServer server = DoipServerImpl.createDoipServer(info);
            RepositoryHandler repositoryHandler = new SimpleRepositoryHandler(info, new DoStorageRocksDBImpl());
            server.setRepositoryHandler(repositoryHandler);
            server.start();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void init() throws Exception {
        if (Security.getProvider("BC") == null) {
            Security.addProvider(new BouncyCastleProvider());
        }

        GlobalConfigurations.JWKPath = jwkPath;
        GlobalConfigurations.IRSAddress = IRSAddress;
        GlobalConfigurations.userID = userID;

        GlobalCertifications.loadKeysFromJWK(GlobalConfigurations.JWKPath);

        GlobalIrpClient.useInternalIrpClient(GlobalCertifications.jwKeyPair
                , GlobalConfigurations.userID
                , GlobalConfigurations.IRSAddress);
    }
}
